package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ToolsPartOutDao;
import com.sugon.dao.ToolsStockDao;
import com.sugon.entity.SysMsgEntity;
import com.sugon.entity.ToolsPartOutEntity;
import com.sugon.entity.ToolsStockEntity;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.service.ToolsPartOutService;
import com.sugon.utils.Constant;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.SysMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 工装--出库记录Service实现类
 *
 * @author sugon
 * @date 2019-08-02 17:51:58
 */
@Service("toolsPartOutService")
public class ToolsPartOutServiceImpl implements ToolsPartOutService {
    @Autowired
    private ToolsPartOutDao toolsPartOutDao;
    @Autowired
    private ToolsStockDao toolsStockDao;
    @Autowired
    private SysProcessInfoService sysProcessInfoService;
    @Autowired
    private SysMsgService sysMsgService;

    @Override
    public ToolsPartOutEntity queryObject(String id) {
        return toolsPartOutDao.queryObject(id);
    }

    @Override
    public List<ToolsPartOutEntity> queryList(Map<String, Object> map) {
        return toolsPartOutDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return toolsPartOutDao.queryTotal(map);
    }

    @Override
    public int save(ToolsPartOutEntity toolsPartOut) {
        toolsPartOut.setId(RandomUtil.randomString(32));
        toolsPartOut.setCreateUser(getUserId());
        toolsPartOut.setCreateDate(new Date());
        toolsPartOut.setApplyTime(new Date());
        toolsPartOut.setApplyUser(getUserId());
        toolsPartOut.setIsDel(Integer.valueOf(Constant.IS_NOT_DEL));
        //添加审核 消息提醒
      /*  sysProcessInfoService.saveProcessInfo(toolsPartOut.getId(), ProcessInfoEnum.SPA_CKSP, 0, null);
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        sysMsgEntity.setDataId(toolsPartOut.getId());
        sysMsgEntity.setDataUrl("../tools/toolspartout.html?erpCode=" + toolsPartOut.getSystemCode());
        sysMsgEntity.setType(SysMsgType.APPROVAL_REMIND);
        sysMsgEntity.setUserId(ShiroUtils.getUserId());
        sysMsgEntity.setDept(ShiroUtils.getDeptId());
        sysMsgEntity.setTitle("工装"+ProcessInfoEnum.SPA_CKSP.getDesc());
        sysMsgEntity.setDataType(ProcessInfoEnum.SPA_CKSP);
        sysMsgService.allMsgData(sysMsgEntity);*/
        return toolsPartOutDao.save(toolsPartOut);
    }

    @Override
    public int update(ToolsPartOutEntity toolsPartOut) {
        toolsPartOut.setUpdateDate(new Date());
        toolsPartOut.setUpdateUser(getUserId());
        return toolsPartOutDao.update(toolsPartOut);
    }

    @Override
    public int delete(String id) {
        return toolsPartOutDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return toolsPartOutDao.deleteBatch(ids);
    }

    @Override
    public int review(ToolsPartOutEntity toolsPartOutEntity) {
        toolsPartOutEntity.setCheckUser(getUserId());
        toolsPartOutEntity.setCreateDate(new Date());
        ToolsStockEntity toolsStockEntity = toolsStockDao.queryByCode(toolsPartOutEntity.getSystemCode());
        int i = 0;
        /**
         * @// TODO: 2019/8/4  出库的时候还没计算库存是否够用, 库存数量 ,安全库存
         */
        /**
         * 审核通过 直接计算库存
         */
        String status = toolsPartOutEntity.getCheckStatus();
        if (status.equals(Constant.IS_ADOPT)) {
            if (toolsStockEntity != null) {
                Integer num = toolsStockEntity.getStockNum() - toolsPartOutEntity.getOutNum();//计算库存
                toolsStockEntity.setStockNum(num);
                toolsStockDao.update(toolsStockEntity);
            }
            i = toolsPartOutDao.update(toolsPartOutEntity);
        }
        sysProcessInfoService.saveProcessInfo(toolsPartOutEntity.getId(), ProcessInfoEnum.SPA_CKSP, Integer.valueOf(toolsPartOutEntity.getCheckUser()), null);
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        sysMsgEntity.setDataId(toolsPartOutEntity.getId());
        sysMsgEntity.setDataType(ProcessInfoEnum.SPA_CKSP);
        sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
        sysMsgService.allMsgData(sysMsgEntity);
        return i;
    }

    @Override
    public int statTotalNum(String posId, String month) {
        Integer totalNum = toolsPartOutDao.statTotalNum(posId, month);
        return totalNum == null ? 0 : totalNum;
    }

}
